Skip to content

[WIP] Propagate upstream gradients in the TensorFlow backend - #924

Draft
linjiaye929 wants to merge 2 commits into
PythonOT:masterfrom
linjiaye929:fix/tensorflow-upstream-gradient
Draft

linjiaye929 wants to merge 2 commits into
PythonOT:masterfrom
linjiaye929:fix/tensorflow-upstream-gradient

Conversation

@linjiaye929

Copy link
Copy Markdown

Types of changes

  • Bug fix

Motivation and context

TensorFlowBackend.set_gradients currently discards the upstream cotangent. Multiplying a loss by zero, a negative coefficient, or a positive coefficient therefore leaves its supplied gradients unchanged. For example, for x=[1,2] and a supplied gradient 2*x, multiplying the returned scalar by 7 gives [2,4] instead of [14,28].

Apply the upstream cotangent to each supplied gradient with tf.nest.map_structure. The forward value, public API, and other backends are unchanged.

Related: #923 explicitly documents this independent TensorFlow backend issue as outside its scope. This PR does not modify dmmot or the JAX work in #917.

Validation and limits

  • Added 11 regression cases: eager and tf.function multi-input chain-rule checks for coefficients 0, 1, -2 and 7, plus emd2 cost-gradient checks for coefficients 0, -2 and 7.
  • Eight direct checks loading the exact current-source backend.py pass. On the unpatched source, the six non-unit-coefficient cases fail and both unit-coefficient controls pass.
  • The exact 11 added pytest cases pass using the released POT 0.9.7.post1 wheel with only TensorflowBackend.set_gradients replaced by this exact current-source method. The three emd2 scaling cases fail using the wheel's original method and pass with the replacement.
  • Environment: Linux CPU, Python 3.12.14, TensorFlow CPU 2.21.0, NumPy 2.5.4, SciPy 1.18.1.
  • pre-commit run --all-files passes all six configured hooks; git diff --check passes.

This is not a full current-source test-suite pass. Building master was blocked when the compiler was killed while compiling the unrelated BSP/Eigen extension, including lower-optimization attempts. Temporary build-option changes were restored and are not part of this PR. The wheel-backed integration checks use released native extensions, not current master native code. The full compatibility matrix, GPU behavior, and remote CI have not been verified.

Keeping this PR as a Draft while that validation remains outstanding.

PR checklist

  • I have read the CONTRIBUTING document.
  • The documentation is up-to-date with the changes I made (check build artifacts).
  • All tests passed, and additional code has been covered with new tests.
  • I have added a release note to RELEASES.md.

The unchecked test box is intentional: targeted checks passed as described above, but the full source suite did not run. No documentation API change is needed; documentation artifacts were not built.

AI assistance

Prepared with AI assistance. The defect, minimal change, and regression checks were independently reviewed by a second assistant; this is not a claim of human or maintainer review.

@codecov

codecov Bot commented Nov 16, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.07%. Comparing base (3367480) to head (16bad15).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master     #924   +/-   ##
=======================================
  Coverage   97.07%   97.07%           
=======================================
  Files         130      130           
  Lines       27095    27124   +29     
=======================================
+ Hits        26302    26331   +29     
  Misses        793      793           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Resolve additive release-note and backend-test conflicts by retaining both
changes. Current merged Python source with POT 0.9.7.post1 native extensions:
47 focused regressions pass; adjacent backend/OT/solver tests: 919 passed,
225 skipped. TensorFlow emd2 three-input scaling probe and all six
pre-commit hooks pass. Local full source build remains blocked by the
compiler being killed in the unrelated BSP/Eigen extension; remote CI is
still needed for the integrated source build.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant